﻿using System;
using System.Windows.Input;
using WickedFlame.Core.Shapes;
using System.Windows;

namespace WickedFlame.Core.Tools
{
	public class RectangleTool : FactoryTool
	{
        public RectangleTool()
            : base()
        {
        }

        protected override VectorShape CreateShape(Point mousePosition)
        {
            var shape = new RectangleShape(new Point((int)mousePosition.X, (int)mousePosition.Y), new Size(1, 1));

            shape.Fill = ShapePropertySettings.Settings.Fill;
            shape.StrokeThickness = ShapePropertySettings.Settings.StrokeThickness;
            shape.Stroke = ShapePropertySettings.Settings.Stroke;

            return shape;
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
		{
            //Add object to history
            //var shape = sender.SelectedLayer.ShapeList[0] as VECRectangle;
            var shape = sender.SelectedLayer.ShapeList.TopShape as RectangleShape;
            if (shape != null)
            {
                sender.PushHistoryCommand(new WickedFlame.Core.History.AddCommand(shape, sender.SelectedLayerIndex));
                shape.CallChangeEndHandler();
            }
		}

        public override void OnMouseMove(DrawingCanvas sender, System.Windows.Input.MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
                //var shape = sender.SelectedLayer.ShapeList[0] as VECRectangle;
                var shape = sender.SelectedLayer.ShapeList.TopShape as RectangleShape;
                if (shape != null)// CanvasManager.SelectedObject != null)
                {
                    Point m = Mouse.GetPosition(sender);

                    if (m.Y > (shape.Y + (shape.Height / 2)) &&
                        m.X > (shape.X + (shape.Width / 2)))//lower right
                        shape.MoveTracker(new Point((int)m.X,(int)m.Y), 3);
                    else if (m.Y > (shape.Y + (shape.Height / 2)) &&
                        m.X < (shape.X + (shape.Width / 2)))//lower left
                        shape.MoveTracker(new Point((int)m.X, (int)m.Y), 4);
                    else if (m.Y < (shape.Y + (shape.Height / 2)) &&
                        m.X < (shape.X + (shape.Width / 2)))//upper left
                        shape.MoveTracker(new Point((int)m.X, (int)m.Y), 1);
                    else//upper right
                        shape.MoveTracker(new Point((int)m.X, (int)m.Y), 2);
                }
			}
		}
	}
}
